﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data.SqlClient;
using System.Data;
using System.Data.Common;

using Data_Objects;
using Business_Objects;
namespace Data_Objects.Implementation
{
    //Ko public -> tất cả gọi qua dataacess
    class SqlServerTieuChuanDinhDuongDao : ITieuChuanDinhDuongDao
    {
        public SqlServerTieuChuanDinhDuongDao()
        {
            SQLUtility.TableName = "TieuChuanDinhDuong";
        }

        //Trả về đối tượng báo ăn
        private TieuChuanDinhDuong MakeTieuChuanDinhDuong(DataRow row)
        {
            Guid _id = Guid.Parse(row["Id"].ToString());
            int _tuoi = int.Parse(row["Tuoi"].ToString());
            Guid _thanhPhanThucPhamId = Guid.Parse(row["ThanhPhanThucPhamId"].ToString());
            double _dinhMuc = double.Parse(row["DinhMuc"].ToString());
            int _schoolID = int.Parse(row["SchoolID"].ToString());

            return new TieuChuanDinhDuong(_id, _tuoi, _thanhPhanThucPhamId, _dinhMuc, _schoolID);
        }

        //Lấy danh sách khách hàng trả về datatable
        private IList<TieuChuanDinhDuong> MakeTieuChuanDinhDuongs(DataTable dt)
        {
            IList<TieuChuanDinhDuong> list = new List<TieuChuanDinhDuong>();
            foreach (DataRow row in dt.Rows)
            {
                list.Add(MakeTieuChuanDinhDuong(row));
            }
            return list;
        }
        private DbParameter[] Take(TieuChuanDinhDuong TieuChuanDinhDuong)
        {
            DbParameter[] parameters = new DbParameter[5];
            parameters[0] = new SqlParameter("@Id", TieuChuanDinhDuong.Id);
            parameters[1] = new SqlParameter("@Tuoi", TieuChuanDinhDuong.Tuoi);
            parameters[2] = new SqlParameter("@ThanhPhanThucPhamId", TieuChuanDinhDuong.ThanhPhanThucPhamId);

            parameters[3] = new SqlParameter("@DinhMuc", TieuChuanDinhDuong.DinhMuc);
            parameters[4] = new SqlParameter("@SchoolID", TieuChuanDinhDuong.SchoolID);
            return parameters;
        }

        public IList<Business_Objects.TieuChuanDinhDuong> GetTieuChuanDinhDuong()
        {
            SQLUtility.TableName = "TieuChuanDinhDuong";
            DbParameter[] param = null;
            DataTable dt = CommandProvider.GetDatatable(SQLUtility.ProceduresGetAll, param);
            return MakeTieuChuanDinhDuongs(dt);
        }

        public Business_Objects.TieuChuanDinhDuong GetTieuChuanDinhDuongByID(string TieuChuanDinhDuongID)
        {
            SQLUtility.TableName = "TieuChuanDinhDuong";
            DbParameter[] parameters = new DbParameter[1];
            parameters[0] = new SqlParameter("@Id", TieuChuanDinhDuongID);
            DataRow row = CommandProvider.GetDataRow(SQLUtility.ProceduresGetByID, parameters);
            if (row == null) { return null; }
            else
            {
                return MakeTieuChuanDinhDuong(row);
            }
        }

        public void InsertTieuChuanDinhDuong(Business_Objects.TieuChuanDinhDuong TieuChuanDinhDuong)
        {
            SQLUtility.TableName = "TieuChuanDinhDuong";
            int newID = CommandProvider.Insert(SQLUtility.ProceduresInsert, Take(TieuChuanDinhDuong), true);
        }

        public int UpdateTieuChuanDinhDuong(Business_Objects.TieuChuanDinhDuong TieuChuanDinhDuong)
        {
            SQLUtility.TableName = "TieuChuanDinhDuong";
            try { return CommandProvider.Update(SQLUtility.ProceduresUpdate, Take(TieuChuanDinhDuong)); }
            catch { return -1; }
        }

        public int DeleteTieuChuanDinhDuong(Business_Objects.TieuChuanDinhDuong TieuChuanDinhDuong)
        {
            SQLUtility.TableName = "TieuChuanDinhDuong";
            try { return CommandProvider.Delete(SQLUtility.ProceduresDelete, Take(TieuChuanDinhDuong)); }
            catch { return -1; }
        }
    }
}
